home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / include / STIMP / pgm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-14  |  5.1 KB  |  216 lines

  1.  
  2. // PGM: declarations and routines
  3. // Version 2.09, 29.Jan.98
  4. // (c) 1997/98 by Stefan Diener
  5.  
  6. #ifndef STIMP_PGM_INC
  7. #define STIMP_PGM_INC
  8.  
  9. #include <STIMP/misc.c>
  10.  
  11. #ifndef NO_PSEUDO_PGM_MODE
  12. #ifndef NO_PSEUDO_PPM_MODE
  13. #define NO_PSEUDO_PPM_MODE
  14. #endif
  15. #include <STIMP/pbm.c>
  16. #include <STIMP/ppm.c>
  17. #endif
  18.  
  19. struct PGM_Info
  20. {
  21.   unsigned int height;
  22.   unsigned int width;
  23.   unsigned int maxval;
  24.   unsigned char *Data;
  25. };
  26.  
  27. int ReadPGMFile(char *name, struct PGM_Info *info)
  28. // read a PGM-file
  29. {
  30.   FILE *datei;
  31.   char tempo[5];
  32.   int i;
  33.   unsigned char *src;
  34.  
  35.   if (strcmp("-",name)==0) datei=stdin;   // use stdin
  36.   else
  37.   {
  38.     // really open a file
  39.     datei=fopen(name,"rb");
  40.     if (datei==0)
  41.     {
  42.       PrintMessage("File %s not found !", name);
  43.       return 1;
  44.     }
  45.   }
  46.  
  47.   // set buffer size
  48.   setvbuf(datei,0,_IOFBF,500);
  49.  
  50.   // check type
  51.   fgets(tempo, 4, datei);
  52.   correct(tempo);
  53.   if (strcmp(tempo,"P5")!=0)
  54.   {
  55.     if (strcmp("-",name)==0)
  56.     {
  57.       PrintMessage("Unknown file type: %s !",name);
  58.       return 1;
  59.     }
  60.     else
  61.     {
  62. #ifndef NO_PSEUDO_PGM_MODE
  63.       int error=0;
  64.       struct PBM_Info infoB;
  65.       struct PPM_Info infoP;
  66.  
  67.       fclose(datei);
  68.       switch (GetType(name))
  69.       {
  70.         case TYPE_PBM: error=ReadPBMFile(name, &infoB);
  71.                                   if (!error)
  72.                                   {
  73.                                     info->width=infoB.width;
  74.                                     info->height=infoB.height;
  75.                                     info->maxval=255;
  76.                                     info->Data=infoB.Data;
  77.                                   }
  78.                                   return error;
  79.                                   break;
  80.  
  81.         case TYPE_PPM: error=ReadPPMFile(name, &infoP);
  82.                                   if (!error)
  83.                                   {
  84.                                     info->width=infoP.width;
  85.                                     info->height=infoP.height;
  86.                                     info->maxval=infoP.maxval;
  87.                                     info->Data=infoP.redData;
  88.                                     free((void *) infoP.greenData);
  89.                                     free((void *) infoP.blueData);
  90.                                   }
  91.                                   return error;
  92.                                   break;
  93.  
  94.         default: return 1;
  95.                      break;
  96.       }
  97. #else
  98.       fclose(datei);
  99.       PrintMessage("Unknown file type: %s !",name);
  100.       return 1;
  101. #endif
  102.     }
  103.   }
  104.  
  105.   // read dimensions and maxval
  106.   // skip comments !!!
  107.   ReadComment(datei);
  108.   fscanf(datei,"%i %i\n",&info->width,&info->height);
  109.   ReadComment(datei);
  110.   fscanf(datei,"%i\n",&info->maxval);
  111.  
  112.   // print it on the screen
  113.   PrintMessage("Reading %s (PGM, %ix%i)", name, info->width, info->height);
  114.  
  115.   // allocate memory for picture data
  116.   info->Data=NULL;
  117.   info->Data=(unsigned char *) malloc(info->height*info->width*sizeof(unsigned char));
  118.   if (info->Data==NULL)
  119.   {
  120.     PrintMessage("No memory for the picture data left !");
  121.     if (strcmp("-",name)!=0) fclose(datei);
  122.     return 1;
  123.   }
  124.  
  125.   // need an empty picture
  126.   src=info->Data;
  127.   for (i=0; i<info->width*info->height; i++) *src++=0;
  128.  
  129.   // read picture data
  130.   i=fread((void *) info->Data, sizeof(unsigned char), info->width*info->height, datei);
  131.   if (i!=(info->width*info->height)) PrintMessage("Error while reading the file !");
  132.  
  133.   // close file
  134.   if (strcmp("-",name)!=0) fclose(datei);
  135.  
  136.   // ok
  137.   return 0;
  138. }
  139.  
  140. int WritePGMFile(char *name, struct PGM_Info *info)
  141. // write a PGM-file
  142. {
  143.   FILE *datei;
  144.   int i;
  145.  
  146.   if (strcmp("-",name)==0) datei=stdout;   // use stdout
  147.   else
  148.   {
  149.     // really open a file
  150.     datei=fopen(name,"wb");
  151.     if (datei==0)
  152.     {
  153.       PrintMessage("Could not open destination file %s !", name);
  154.       return 1;
  155.     }
  156.   }
  157.  
  158.   PrintMessage("Writing %s (PGM, %ix%i)", name, info->width, info->height);
  159.  
  160.   // set buffer size
  161.   setvbuf(datei,0,_IOFBF,500);
  162.  
  163.   // write file header
  164.   fprintf(datei,"P5\n");
  165.   fprintf(datei,"# written by: "OP_NAME" "VERSION" ("DATE"), author: "AUTHOR"\n");
  166.   fprintf(datei,"%i %i\n",info->width,info->height);
  167.   fprintf(datei,"%i\n",info->maxval);
  168.  
  169.   // write picture data
  170.   i=fwrite((void *) info->Data, sizeof(unsigned char), info->width*info->height, datei);
  171.   if (i!=(info->width*info->height)) PrintMessage("Error while writing the file !");
  172.  
  173.   // flush buffer and close file
  174.   fflush(datei);
  175.   if (strcmp("-",name)!=0) fclose(datei);
  176.  
  177.   // ok
  178.   return 0;
  179. }
  180.  
  181. int CreatePGMArray(int y, int x, struct PGM_Info *info)
  182. // allocate memory for a PGM-picture
  183. {
  184.   int i;
  185.   unsigned char *dst;
  186.  
  187.   // save dimensions and maxval
  188.   info->width=x;
  189.   info->height=y;
  190.   info->maxval=255;
  191.  
  192.   // allocate memory
  193.   info->Data=NULL;
  194.   info->Data=(unsigned char *) malloc(x*y*sizeof(unsigned char));
  195.   if (info->Data==NULL)
  196.   {
  197.     PrintMessage("No memory for picture data left !");
  198.     return 1;
  199.   }
  200.  
  201.   // need an empty picture
  202.   dst=info->Data;
  203.   for (i=0; i<info->width*info->height; i++) *dst++=0;
  204.  
  205.   // ok
  206.   return 0;
  207. }
  208.  
  209. void FreePGMArray(struct PGM_Info *info)
  210. // free memory of a PGM-picture
  211. {
  212.   free((void *) info->Data);
  213. }
  214.  
  215. #endif
  216.